package com.example.lanouhn.bantangapp.activities.activities;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.lanouhn.bantangapp.R;
import com.example.lanouhn.bantangapp.activities.fragments.Fragment_Avatar;
import com.example.lanouhn.bantangapp.activities.fragments.Fragment_Communtiy;
import com.example.lanouhn.bantangapp.activities.fragments.Fragment_Msg;
import com.example.lanouhn.bantangapp.activities.fragments.Fragment_Selected;
import com.example.lanouhn.bantangapp.activities.fragments.Fragment_main;
import com.example.lanouhn.bantangapp.activities.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 主页面
 */
public class MainActivity extends FragmentActivity {

    private FrameLayout fram_main;
    private RadioGroup rg;
    //viewpager的数据集合
    private List<Fragment> fragments;

    private String  key;//判断是否登录的标识
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initview();
        initLinister();
        initData();
    }

    //数据集合
    private void initData() {
        //Fragment集合
        fragments = new ArrayList();
        // 添加页面
        fragments.add(new Fragment_main());
        fragments.add(new Fragment_Msg());
        fragments.add(new Fragment_Avatar());
        fragments.add(new Fragment_Communtiy());
    }

    //找到控件
    private void initview() {
        setContentView(R.layout.activity_main);
        fram_main = (FrameLayout) findViewById(R.id.fram_main);
        rg = (RadioGroup) findViewById(R.id.rg);

        key= (String) SPUtils.get(MainActivity.this,"login","");

        FragmentManager me = getSupportFragmentManager();
        FragmentTransaction tran = me.beginTransaction();
        tran.replace(R.id.fram_main, new Fragment_main()).commit();
    }

    //点击事件
    private void initLinister() {

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.selected:
                        FragmentManager me = getSupportFragmentManager();
                        FragmentTransaction tran = me.beginTransaction();
                        tran.replace(R.id.fram_main, new Fragment_main()).commit();

                        break;
                    case R.id.rcommunuty:
                        FragmentManager me1 = getSupportFragmentManager();
                        FragmentTransaction tran1 = me1.beginTransaction();
                        tran1.replace(R.id.fram_main, new Fragment_Communtiy()).commit();

                        break;
                    case R.id.rpublish:
                        Intent it = new Intent(MainActivity.this, PublishActivity.class);
                        startActivity(it);
                        break;
                    case R.id.rmsg:
                        if (key=="")
                        {
                            Intent it1 = new Intent(MainActivity.this,ActivityLand.class);
                            startActivity(it1);
                        }else {
                            FragmentManager me2 = getSupportFragmentManager();
                            FragmentTransaction tran2 = me2.beginTransaction();
                            tran2.replace(R.id.fram_main, new Fragment_Msg()).commit();
                        }
                        break;
                    case R.id.ravatar:
                        if (key=="")
                        {
                            Intent it1 = new Intent(MainActivity.this,ActivityLand.class);
                            startActivity(it1);
                        }else {
                            FragmentManager me3 = getSupportFragmentManager();
                            FragmentTransaction tran3 = me3.beginTransaction();
                            tran3.replace(R.id.fram_main, new Fragment_Avatar()).commit();
                        }
                        break;
                }
            }
        });
    }

    /**
     * 点击两次退出
     */

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        exitB2Click();
        return false;
    }

    private static boolean isExit = false;

    private void exitB2Click() {

        //用来记录两次点击返回键的时间
        Timer tExit = null;
        if (isExit == false) {
            isExit = true;//准备退出
            Toast.makeText(MainActivity.this, "再点一次就退出了哦", Toast.LENGTH_SHORT).show();
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {

                    isExit = false;//取消退出

                }
            }, 2000);


        } else {

            finish();

        }

    }
}
